#!/bin/bash
function install_jdk() {
    echo -e "${blue}当前正在执行的操作为:${nocolor} ${green}安装JDK${nocolor}"
    echo ""
    echo -ne "输入JDK安装位置[/usr/local]: "
    read -e jdkPath
    jdkPath=${jdkPath:-/usr/local}
    jdkPath=`echo $jdkPath`
    jdkPath=${jdkPath%/}
    echo ""
    
    jdk_MainVer=
    while [[ ! $jdk_MainVer =~ ^[1-9]+[0-9]*$ ]]
    do
        echo -ne "输入JDK主版本号，如6,7,8,9...[7]: "
        read jdk_MainVer
    jdk_MainVer=${jdk_MainVer:-7}
        if [[ ! $jdk_MainVer =~ ^[1-9]+[0-9]*$ ]];then
           echo -e "$Warn JDK大版本号必须为不以0开头的数字，请重新输入！"
           sleep 0.3
        fi
    done
    echo ""
    
    find $workHome/packages/jdk/* 2>/dev/null -maxdepth 0 -type d -exec rm -rf {} \;
    jdkPack=`ls -r $workHome/packages/jdk/ 2>/dev/null|grep -i "jdk-${jdk_MainVer}"|head -n1`
    
    if [ -z "${jdkPack}" ];then
        if [ "${die_network}" == "true" ];then
            echo -e "$Error 未找到任何安装包，且主机未联网。请确认packages/jdk/下有JDK${jdk_MainVer}安装包，\n且安装包名称为jdk-<主版本u小版本>格式。退出安装！"
            anykeyback
            return 1
        else
            main_download_url="https://sosnb-generic.pkg.coding.net/solo/jdk"
            case ${jdk_MainVer} in
            6)
                download_pkg="jdk-6u45-linux-x64.bin"
                ;;
            7)
                download_pkg="jdk-7u80-linux-x64.tar.gz"
                ;;
            8)
                download_pkg="jdk-8u162-linux-x64.tar.gz"
                ;;
            11)
                download_pkg="jdk-11.0.12_linux-x64_bin.tar.gz"
                ;;
            *)
                download_pkg="None"
                ;;
            esac
            
            if [ "${download_pkg}" != "None" ];then
                msg="下载JDK安装包中，此过程会持续5分钟以上，请等待......"
                echo -e "XXX\n22\n$msg\nXXX"
                echo 22
                curl --progress-bar --connect-timeout 30 -m 600 -fL -s --fail "${main_download_url}/${download_pkg}" -o $workHome/packages/jdk/${download_pkg} >>$logs_file 2>&1
                exit_status="$?"
                if [ "${exit_status}" != "0" ];then
                    echo -e "$Error 下载安装包失败"
                    anykeyback
                    return 1
                fi
            else
                echo -e "${Error} 不支持下载的版本，请手动下载安装包！"
            fi
        fi
    fi
    
    jdkPack=`ls -r $workHome/packages/jdk/ 2>/dev/null|grep -i "jdk-${jdk_MainVer}"|head -n1`
    jdkPack_=${jdkPack##*.}
    if [ "$jdkPack_" == "bin" ];then
        unPack_command="sh"
    elif [ "$jdkPack_" == "gz" ];then
        unPack_command="tar zxf"
    elif [ "$jdkPack_" == "bz2" ];then
        unPack_command="tar jxf"
    else
        echo -e "$Error 不支持的安装包格式，请使用bin、tar.gz、tar.bz2格式的二进制安装包\n可通过下面的网址去下载\nhttps://www.oracle.com/java/technologies/oracle-java-archive-downloads.html"
        anykeyback
        return 1
    fi
    jdk_detailVer=`echo $jdkPack|grep -Eo "${jdk_MainVer}u[0-9]+"`

    echo -ne "${blue}JDK安装信息汇总${nocolor}
---------------------------------
JDK主版本：$jdk_MainVer
JDK具体版本：JDK-$jdk_detailVer
安装包：$jdkPack
安装位置：$jdkPath
---------------------------------
继续[Yy,Enter],退出[Nn]: "
    read isYn
    [[ $isYn != [Nn] ]] || { sleep 0.5;return 1; }
    
    echo ""
    echo -e "${cyan}将开始JDK安装过程${nocolor}"
    echo ""
    echo -e "$Info ${purple}检查是否有openjdk并卸载${nocolor}"
    rpm -qa |grep openjdk>/dev/null 2>&1
    if [ "$?" == "0" ];then
        yum -y remove java-1.*-openjdk
    fi
    echo ""
    
    echo -e "$Info ${purple}解压安装包${nocolor}"
    find $workHome/packages/jdk/* 2>/dev/null -maxdepth 0 -type d -exec rm -rf {} \;
    cd $workHome/packages/jdk/
    $unPack_command $jdkPack
    if [ "$?" != "0" ];then
        echo -e "$Error 解压失败，检查安装包完整性！退出安装"
        anykeyback
        return 1
    fi
    unPack_name=`ls -F|grep "\/"`
    unPack_name=${unPack_name%/}
    echo ""
    
    echo -e "$Info ${purple}移动文件到安装位置$nocolor"
    if [ ! -d "$jdkPath" ];then
        mkdir -p $jdkPath
    fi
    if [ -n "$unPack_name" ];then
        rm -rf $jdkPath/$unPack_name
    else
        echo -e "$Error 未找到解压后的文件夹，复制文件到安装目录失败，退出安装！"
        anykeyback
        return 1
    fi
    /bin/mv -f $unPack_name $jdkPath/
    echo ""
    
    echo -e "$Info ${purple}配置环境变量${nocolor}"
    sed -i "/JAVA_HOME=/d" /etc/profile
    #sed -i "/CLASSPAT=.:$JAVA_HOME/d" /etc/profile
    cat /etc/profile 2>/dev/null|grep 'PATH=$JAVA_HOME/bin.*:$JAVA_HOME/jre/bin.*' >/dev/null 2>&1
    exist_java_path="$?"
    cat /etc/profile 2>/dev/null|grep 'CLASSPAT=.*$JAVA_HOME/lib.*' >/dev/null 2>&1
    exist_java_clss="$?"
    
    class_line=`sed -n -e '/CLASSPAT\=.*$JAVA_HOME.*/=' /etc/profile|head -n1`
    path_line=`sed -n -e '/PATH\=.*$JAVA_HOME\/bin.*/=' /etc/profile|head -n1`
    if [ -n "$class_line" -a -n "$path_line" ];then
        if [ "$class_line" -lt "$path_line" ];then
          sed -i "${class_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile
        else
          sed -i "${path_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile
        fi
    elif [ -n "$class_line" -a -z "$path_line" ];then
        sed -i "${class_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile
    elif [ -z "$class_line" -a -n "$path_line" ];then
        sed -i "${path_line}iexport JAVA_HOME=$jdkPath/$unPack_name" /etc/profile
    else
        echo "export JAVA_HOME=$jdkPath/$unPack_name">>/etc/profile
    fi
    
    if [ "$exist_java_path" != "0" -a "$exist_java_clss" != "0" ];then
        echo "export CLASSPAT=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib">>/etc/profile
        sed -i 's#:$JAVA_HOME/bin:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin/:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin/:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin##g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin##g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin/##g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin/##g' /etc/profile
        echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH">>/etc/profile
    elif [ "$exist_java_path" != "0" -a "$exist_java_clss" == "0" ];then
        sed -i 's#:$JAVA_HOME/bin:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin/:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin/:#:#g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin##g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin##g' /etc/profile
        sed -i 's#:$JAVA_HOME/bin/##g' /etc/profile
        sed -i 's#:$JAVA_HOME/jre/bin/##g' /etc/profile
        echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH">>/etc/profile
    elif [ "$exist_java_path" == "0" -a "$exist_java_clss" != "0" ];then
        echo "export CLASSPAT=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib">>/etc/profile
    fi
    #修改urandom
    if [ -f $jdkPath/$unPack_name/jre/lib/security/java.security ];then
        sed -i 's#^securerandom.source=file:/dev/urandom#securerandom.source=file:/dev/./urandom#g' $jdkPath/$unPack_name/jre/lib/security/java.security
    fi
    source /etc/profile
    
    sleep 0.15
    #判断是否安装成功 
    installd_jdkVer=$(${jdkPath}/${unPack_name}/bin/java -version 2>&1|grep "java version"|awk '{print $3}'|sed 's/"//g')
    jdkVer=`java -version 2>&1|grep "java version"|awk '{print $3}'|sed 's/"//g'`
    
    echo ""
    if [ "${installd_jdkVer}" == "${jdkVer}" ];then
        echo -e "$Info ${light_green}JDK $jdkVer安装成功!${nocolor}"
    else
        echo -e "$Error JDK $jdk_MainVer安装失败，请检查日志文件\n检查环境变量文件/etc/profile\n如果环境变量未生效，请重新登录尝试"
    fi
    anykeyback
}